package com.power.sifa.approve.dao;

import com.power.sifa.approve.model.Apl_admreview;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 行政复议申请 持久层(hibernate)
 */

public interface Apl_admreviewDao extends JpaRepository<Apl_admreview, Serializable>, JpaSpecificationExecutor<Apl_admreview>, Repository<Apl_admreview, Serializable>, Apl_admreviewDaoCustom<Apl_admreview, Serializable> {

   @Modifying
   @Transactional
   @Query("delete  from Apl_admreview s where  s.id in (:ids) ")
   void removeByIds(@Param("ids") List<Integer> ids);


   /**
    * 查询相似度的行政复议数据
    * @param applyId  对应的主表的id
    * @return
    */
   @Query(value = "select M.applycode,M.regusername,M.createtime,M.acceptcode,T.id as applyid,T.applyname,T.targetname,T.reason, T.status from apl_admreview T left join  apl_main M on M.id = T.id  where T.id in (select " +
           " distinct applyid from apl_applicant A join (select distinct cardno from apl_applicant where applyid " +
           "=:applyId)  B on A.cardno = B.cardno where A.applyid != :applyId intersect (select distinct applyid from " +
           "apl_target B join (select distinct admunitid from apl_target where applyid =:applyId ) C on B.admunitid = C.admunitid " +
           "where B.applyid != :applyId)) or (T.id = :applyId)", nativeQuery = true)
   List<Map<String, Object>>  getSimilarityReview(String applyId);

   /**
    * 更新业务主表状态
    * @param id
    * @param status
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.status = :status where  s.id = :id ")
   void updateStatus(@Param("id") String id, @Param("status") Integer status);

   /**
    * 更新业务主表子状态
    * @param id
    * @param sonstatus
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.sonstatus = :sonstatus where  s.id = :id ")
   void updatesSonStatus(@Param("id") String id, @Param("sonstatus") String sonstatus);


   /**
    * 更新业务主表决定类型
    * @param id
    * @param decisiontype
    * @param childId
    * @param childName
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.decisiontype = :decisiontype, s.decisiontypeid = :childId, s.decisiontypename = :childName where  s.id = :id ")
   void updatesDecisiontype(@Param("id") String id, @Param("decisiontype") Integer decisiontype,@Param("childId") Integer childId, @Param("childName") String childName);

   /**
    * 更新受理第一/第二 立案审查人
    * @param id
    * @param firstuserid
    * @param firstusername
    * @param seconduserid
    * @param secondusername
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.acceptfirstid = :firstuserid, s.acceptfirstname = :firstusername, s.acceptsecondid = :seconduserid, s.acceptsecondname = :secondusername  where  s.id = :id ")
   void updatesAcceptExamineUsers(String id, String firstuserid, String firstusername, String seconduserid, String secondusername);

   /**
    * 更新审理第一/第二 立案审查人
    * @param id
    * @param firstuserid
    * @param firstusername
    * @param seconduserid
    * @param secondusername
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.hearfirstid = :firstuserid, s.hearfirstname = :firstusername, s.hearsecondid = :seconduserid, s.hearsecondname = :secondusername  where  s.id = :id ")
   void updatesHearExamineUsers(String id, String firstuserid, String firstusername, String seconduserid, String secondusername);

   /**
    * 更新业务主表程序类型
    * @param id
    * @param proctype
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.proctype = :proctype where  s.id = :id ")
   void updateProctype( String id, Integer proctype);

   /**
    * 更新业务主表决定时间
    * @param id
    * @param decisiondate
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.decisiondate = :decisiondate where  s.id = :id ")
   void updateDecisiondate( String id, String decisiondate);


   /**
    * 根据申请人、被申请人查询类案
    * @param applyname
    * @param targetname
    * @return
    */
   @Query("from Apl_admreview s where s.id <> :applyid and s.applyname = :applyname or s.targetname = :targetname ")
   List<Apl_admreview> querySimilarCase(String applyid, String applyname, String targetname);

   /**
    * 更新业务主表期满日期
    * @param id
    * @param quedate
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.quedate = :quedate where  s.id = :id ")
   void updateQuedate( String id, String quedate);

   /**
    * 更新承办人
    * @param id
    * @param firstuserid
    * @param firstusername
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.acceptfirstid = :firstuserid, s.acceptfirstname = :firstusername  where  s.id = :id ")
   void updatesFirstUser(String id, String firstuserid, String firstusername);

   /**
    * 更新协办人
    * @param id
    * @param seconduserid
    * @param secondusername
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.acceptsecondid = :seconduserid, s.acceptsecondname = :secondusername  where  s.id = :id ")
   void updatesSecondUser(String id, String seconduserid, String secondusername);


   /**
    * 查询全部完结的案件数据
    * @param status
    * @return
    */
   @Query(value = "select b.* from apl_main a left join apl_admreview b on a.id = b.id where b.id is not null and a.status in (:status)", nativeQuery = true)
   List<Apl_admreview> findAllEndedList(List<String> status);



   /**
    * 更新审理承办人
    * @param id
    * @param hearfirstid
    * @param hearfirstname
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.hearfirstid = :hearfirstid, s.hearfirstname = :hearfirstname  where  s.id = :id ")
   void updatesHearFirstUser(String id, String hearfirstid, String hearfirstname);

   /**
    * 更新审理协办人
    * @param id
    * @param hearsecondid
    * @param hearsecondname
    */
   @Modifying
   @Transactional
   @Query("update Apl_admreview s set s.hearsecondid = :hearsecondid, s.hearsecondname = :hearsecondname  where  s.id = :id ")
   void updatesHearSecondUser(String id, String hearsecondid, String hearsecondname);


   @Modifying
   @Transactional
   @Query(" from Apl_admreview s where  s.id in (:ids) ")
   List<Apl_admreview> findAdmreviewList(List<String> ids);
}
